﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ueEngine.Framework.Caching;
using ueEngine.Framework.Common;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace ueEngine.Biz.Core.AppService.Support
{
    public class HitManager
    {

    }

    class HitManagerFactory
    { 
        
    }

    #region
    public abstract class HitManagerBehaviour<T>
        where T:class
    {
        private int _Expire = 10;
        public int Expire 
        {
            get { return _Expire; }
            set { _Expire = value; }
        }
        private Action<string, object, CacheItemRemovedReason> cacheitemRemovedAction;
        public HitManagerBehaviour(string keyformat)
        {
            this.KeyFormat = keyformat;
            cacheitemRemovedAction = SetHandle();
        }
        public string KeyFormat { get; set; }
        protected abstract string SerializeKey(params string[] keys);
        protected abstract T LoadData(params string[] keys);
        protected abstract Action<string, object, CacheItemRemovedReason> SetHandle();
        /// <summary>
        /// 获取缓存中的原始数据，原始数据是用CacheWrap&lt;T&gt;包裹的对象
        /// </summary>
        /// <param name="keys"></param>
        /// <returns></returns>
        private object BaseGet(params string[] keys)
        {
            string key = SerializeKey(keys);
            return ueCache.GetCache(key);
        }
        public T Get(params string[] keys)
        {
            object data = BaseGet(keys);
            if (data == null)
            {
                data = LoadData(keys);
                if (data != null && data is T)
                {
                    return Add(data as T, keys);
                } 
                else 
                {
                    return default(T);
                }
            }
            if (data is CacheWrap<T>)
            {
                return (data as CacheWrap<T>).Data;
            }
            return default(T);
        }
        public T Add(T data, params string[] keys)
        {
            string key = SerializeKey(keys);
            return BaseAdd(
                new CacheWrap<T>() { Data = data, Expire = DateTime.Now.AddMinutes(Expire) },
                key);
        }
        private T BaseAdd(CacheWrap<T> data, string key)
        {
            return ueCache.Add<CacheWrap<T>>(key,
                data,
                cacheitemRemovedAction,
                data.Expire).Data;
        }
        public T Change(Func<T,T> changeAction, params string[] keys)
        {
            string key = SerializeKey(keys);
            object data = BaseGet(keys);
            if (data != null)
            {
                CacheWrap<T> cacheitem = data as CacheWrap<T>;
                cacheitem.Data = changeAction.Invoke(cacheitem.Data);
                return BaseAdd(cacheitem, key);
            }
            return default(T);   
        }
    }
    /// <summary>
    /// 对兼职评价情况的统计
    /// </summary>
    public class StatisticsDiscussOfRecruitment : HitManagerBehaviour<DiscussOfRecruitmentData>
    {
        public StatisticsDiscussOfRecruitment(string keyformat)
            : base(keyformat)
        { }
        protected override string SerializeKey(params string[] keys)
        {
            throw new NotImplementedException();
        }

        protected override DiscussOfRecruitmentData LoadData(params string[] keys)
        {
            throw new NotImplementedException();
        }

        protected override Action<string, object, CacheItemRemovedReason> SetHandle()
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// 对代理人评价情况的统计
    /// </summary>
    public class StatisticsDiscussOfAgent : HitManagerBehaviour<DiscussOfAgentData>
    {
        public StatisticsDiscussOfAgent(string keyformat)
            : base(keyformat)
        { }

        protected override string SerializeKey(params string[] keys)
        {
            throw new NotImplementedException();
        }

        protected override DiscussOfAgentData LoadData(params string[] keys)
        {
            throw new NotImplementedException();
        }

        protected override Action<string, object, CacheItemRemovedReason> SetHandle()
        {
            throw new NotImplementedException();
        }
    }
    /// <summary>
    /// 针对评论的反馈。赞同或则反对
    /// </summary>
    public class StatisticsFeedback : HitManagerBehaviour<FeedbackData>
    {
        public StatisticsFeedback(string keyformat)
            : base(keyformat)
        { }

        protected override string SerializeKey(params string[] keys)
        {
            throw new NotImplementedException();
        }

        protected override FeedbackData LoadData(params string[] keys)
        {
            throw new NotImplementedException();
        }

        protected override Action<string, object, CacheItemRemovedReason> SetHandle()
        {
            throw new NotImplementedException();
        }
    }
    #endregion

    public enum ManageModule
    { 
        /// <summary>
        /// 针对兼职的评论
        /// </summary>
        DiscussOfRecruitment,
        /// <summary>
        /// 针对代理人的评论
        /// </summary>
        DiscuuOfAgent,
        /// <summary>
        /// 对兼职评论的反馈
        /// </summary>
        FeedbackOfRecruitmentDis,
        /// <summary>
        /// 针对代理人的评价反馈
        /// </summary>
        FeedbackOfAgentDis
    }
    public class CacheWrap<T>
        where T:class
    {
        public T Data { get; set; }
        public DateTime Expire { get; set; }
    }

    public class DiscussOfRecruitmentData
    { }
    public class DiscussOfAgentData
    { }
    public class FeedbackData
    { }
}
